package mysql

import (
	"vip-shop-hub/viprpc/viprpc"

	"gorm.io/gorm"
)

type Product struct {
	gorm.Model
	ProductName  string  `gorm:"type:varchar(100);not null;commit:'手机号'"`
	ProductPrice float64 `gorm:"type:decimal(10,2);not null;commit:'手机号'"`
	ProductImage string  `gorm:"type:varchar(100);not null;commit:'手机号'"`
	ProductStock string  `gorm:"type:varchar(100);not null;commit:'手机号'"`
	UserId       int64   `gorm:"type:char(11);not null;commit:'手机号'"`
}

func (p *Product) ProuAdd(db *gorm.DB) error {
	return db.Debug().Create(p).Error
}

func List(db *gorm.DB, in *viprpc.ProductListRequest) ([]*viprpc.ProductList, error) {
	var list []*viprpc.ProductList
	db.Debug().Model(&Product{}).Select("products.id as product_id", "products.product_name",
		"products.product_price", "products.product_image", "products.product_stock",
		"users.id as user_id", "users.moble", "users.age", "users.address", "users.`password`", "users.email", "users.head_image", "users.nick_name").
		Joins("JOIN users ON products.user_id=users.id").Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list, err
}
